<?php
/**
 * Created by PhpStorm.
 * User: tommy
 * Date: 2018/6/15
 * Time: 10:31
 */

namespace frontend\service;
use common\models\info\Article;
use common\models\info\Category;
use common\models\info\Tags;
use common\models\base\Admin;
use common\models\info\TagsRelation;
class ArticleService
{
    public static function getIndexData($page,$pageSize)
    {
        $result = Article::arrHotArticle('', $page, $pageSize, '');     //最新文章
        return $result;
    }
    public static function getArticleBySearch(){

    }
    /*
    * 获取文章详细信息
    * @param $aid
    * @return mixed
    */
    public static function getArticleDetail($aid) {
        $data = Article::loadReleaseArticle($aid);
        $data['author_name'] = Admin::getFieldById($data['author_id'],'username');
        if (!$data) {
            throw new CommonException("我访问的页面被外星人劫持了！");
        }
        Article::viewerIncrement($aid);
        $data["tags"]  = TagsRelation::getTagInfoByArtId($aid);
        $data['article'] = Article::getSimilarArticle($aid);
        return $data;
    }


    /****
     * 详情页获取数据
     */
    public static function getDetailArticle($aid){
         $result =[
             'data' => ArticleService::getArticleDetail($aid),
             'randomArticle' => Article::arrRandomRecommend(0, '', 1, 5),   //热门文章
              'hotArticle' =>  Article::arrHotArticle('hot', 1, 7, '')      //z最新文章
         ];
       return $result;
    }
    /***
     * 获取右侧信息
     */
    public  static function getRightMessage(){
         $result =[
             'randomArticle' => Article::arrHotArticle('random', 1, 5, ''),   //热门文章
             'hotArticle' =>  Article::arrHotArticle('hot', 1, 5, ''),      //z最新文章
             'tagBubble' =>Tags::getAllTag(),
         ];
        return $result;
    }

    public static function getDataType(){
        $result = Category::findAll('');
        $resultArr =[];
        foreach ($result as $kk =>$row) {
            $total = Article::find()->where('cate_id='.$row['id'])->count();
            $resultArr[$row['id']] =[
                'rows'=>Article::getDataByType($row['id'],20)['data'],
                'total'=>$total
            ];

        }
        return $resultArr;
     }
    public static function getDataCate($cate_id,$page=1,$pageSize=20){
             $result = Article::findArticleByCate($cate_id,$page,$pageSize);
             return $result;
    }

}
